package com.nega.modules.thread;

import com.nega.commons.model.SearchResult;
import com.nega.commons.spider.IPSourcePool;
import com.nega.commons.spider.SpiderCoreService;
import lombok.extern.slf4j.Slf4j;
import org.jsoup.nodes.Document;
import org.jsoup.nodes.Element;
import org.jsoup.select.Elements;

import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;
import java.util.concurrent.Callable;

/**
 * @Author: Garcia
 * @CreateDate: 2019/5/13 18:36
 * @Description: what it is class?
 */
@Slf4j
public class SpiderThread360 implements Callable<List<SearchResult>> {

    private              SpiderCoreService spiderCoreService;
    private              IPSourcePool      ipSourcePool;
    private              int               num;
    private              String            text;
    private static final String            DOMAIN  = "https://www.so.com";
    private static final String            CONTEXT = "/s";

    public SpiderThread360() {

    }

    public SpiderThread360(SpiderCoreService spiderCoreService, IPSourcePool ipSourcePool, String text, int num) {
        this.num = num;
        this.spiderCoreService = spiderCoreService;
        this.text = text;
        this.ipSourcePool = ipSourcePool;
    }

    @Override
    public List<SearchResult> call() {
        List<SearchResult> results = new ArrayList<>();
        Map<String, String> params = getParams(text);
        int size = (num * 10);
        String ip = ipSourcePool.getIp();
        for (int i = (size - 9); i <= size; i++) {
            params.put("pn", String.valueOf(i));
            Document document = spiderCoreService.getDocument(DOMAIN + CONTEXT, params, ip);
            results.addAll(getInfo(document, i));
            if (isLastPage(document)) {
                break;
            }
        }
        return results;
    }

    private Map<String, String> getParams(String text) {
        Map<String, String> params = new HashMap<>();
        params.put("q", text);
        params.put("psid", "73fed7be9d8d82cbe4bac3755fd148a7");
        params.put("src", "srp_paging");
        params.put("fr", "fr");
        return params;
    }

    private boolean isLastPage(Document document) {
        if (document == null) {
            return true;
        }
        try {
            Element element = document.getElementById("page");
            if (element == null){
                return true;
            }
            Elements elements = element.getElementsByTag("a");
            for (Element ele : elements) {
                if ("下一页".equals(ele.text())) {
                    return false;
                }
            }
        } catch (Exception e) {
            log.error("360检测下一页异常", e);
        }
        return true;
    }

    private List<SearchResult> getInfo(Document document, int page) {
        List<SearchResult> results = new ArrayList<>();
        if (document == null) {
            return results;
        }
        Element main = document.getElementById("main");
        Element resul = main.getElementsByAttributeValue("class", "result").get(0);
        Elements list = resul.getElementsByAttributeValue("class", "res-list");
        for (int i = 0; i < list.size(); i++) {
            Element res = list.get(i);
            try {
                SearchResult result = new SearchResult();
                if(res.getElementsByClass("res-title").size()<1){
                    continue;
                }
                result.setKeyWord(text);
                Element h3 = res.getElementsByClass("res-title").get(0);
                Element h3_a = h3.getElementsByTag("a").get(0);
                result.setUrl(h3_a.attr("href"));
                result.setTitle(h3_a.text());
                Elements content = res.getElementsByClass("res-desc");
                if (content != null && content.size() > 0) {
                    result.setContent(content.get(0).text());
                }
                content = res.getElementsByClass("res-rich");
                for (Element desc : content) {
                    Elements commcon = desc.getElementsByClass("res-comm-con");
                    if (commcon != null && commcon.size() > 0) {
                        result.setContent(commcon.get(0).text());
                        break;
                    }
                    result.setContent(desc.text());
                }

                Elements res_gray = res.getElementsByClass("gray");
                for (Element gray : res_gray) {
                    String time = gray.text();
                    if (!time.contains("年")) {
                        continue;
                    }
                    if (time.contains("：")) {
                        String times[] = time.split("：");
                        result.setTime(times[1]);
                    } else {
                        result.setTime(time);
                    }
                }
                results.add(result);
            } catch (Exception e) {
                log.error("360抓取第[{}]页，显示排位第[{}]位异常", page, i, e);
            }
        }
        return results;
    }

}
